package com.atguigu.spzx.product.service.Impl;

import com.alibaba.fastjson.JSON;
import com.atguigu.spzx.model.dto.api.ApiProductSearchDTO;
import com.atguigu.spzx.model.dto.product.ProductVo2DTO;
import com.atguigu.spzx.model.entity.product.Category;
import com.atguigu.spzx.model.entity.product.Product;
import com.atguigu.spzx.model.entity.product.ProductDetails;
import com.atguigu.spzx.model.entity.product.ProductSku;
import com.atguigu.spzx.model.vo.product.ProductItemVO;
import com.atguigu.spzx.model.vo.product.ProductSpecVO;
import com.atguigu.spzx.model.vo.product.ProductVo2;
import com.atguigu.spzx.product.mapper.CategoryMapper;
import com.atguigu.spzx.product.mapper.ProductDetailsMapper;
import com.atguigu.spzx.product.mapper.ProductMapper;
import com.atguigu.spzx.product.mapper.ProductSkuMapper;
import com.atguigu.spzx.product.service.ProductService;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageInfo;
import jakarta.annotation.Resource;
import org.apache.xmlbeans.impl.xb.xsdschema.Public;
import org.springframework.beans.BeanUtils;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author LT
 * @create 2024-03-25-18:09
 */
@Service
public class ProductServiceImpl extends ServiceImpl<ProductMapper,Product> implements ProductService {

    @Resource
    ProductSkuMapper productSkuMapper;

    @Resource
    CategoryMapper categoryMapper;

    @Resource
    ProductDetailsMapper productDetailsMapper;

    @Resource
    ProductMapper productMapper;

    @Override
    @Cacheable(value = "spzx:index:loadData",key = "'loadDataMap'",unless = "#result.size()==0")
    public Map<String, Object> loadData() {
//        Map<String, Object> redismap = (Map<String, Object>) redisTemplate.opsForValue().get("spzx:index:loadData");
//        if (redismap != null) {
//            return redismap;
//        }

        LambdaQueryWrapper<Category> catQueryWrapper = new LambdaQueryWrapper<>();
        catQueryWrapper.eq(Category::getParentId, 0)
                .orderByAsc(Category::getOrderNum);
        List<Category> categories = categoryMapper.selectList(catQueryWrapper);

        LambdaQueryWrapper<ProductSku> productSkuWrapper = new LambdaQueryWrapper<>();
        productSkuWrapper.eq(ProductSku::getStatus, 1)
                .orderByDesc(ProductSku::getSaleNum)
                .last("limit 10");
        List<ProductSku> productSkuList = productSkuMapper.selectList(productSkuWrapper);

        Map<String, Object> map = new HashMap<>();
        map.put("productSkuList", productSkuList);
        map.put("categoryList", categories);

       // redisTemplate.opsForValue().set("spzx:index:loadData", map);
        return map;
    }

    @Override
    public PageInfo<ProductSku> list(Integer pageNum, Integer pageSize, ApiProductSearchDTO apiProductSearchDTO) {

       Page<ProductSku> page = new Page<>(pageNum,pageSize);
       QueryWrapper queryWrapper = Wrappers.query(ProductSku.class);
       Long brandId = apiProductSearchDTO.getBrandId();
       Long category1Id = apiProductSearchDTO.getCategory1Id();
       Long category2Id = apiProductSearchDTO.getCategory2Id();
       Long category3Id = apiProductSearchDTO.getCategory3Id();
       String keyword = apiProductSearchDTO.getKeyword();
       Integer order = apiProductSearchDTO.getOrder();

       if (brandId!=null){
           queryWrapper.eq("t2.brand_id",brandId);
       }
       if (category1Id!=null){
           queryWrapper.eq("t2.category1_id",category1Id);
       }
       if (category2Id!=null){
           queryWrapper.eq("t2.category2_id",category2Id);
       }
       if (category3Id!=null){
           queryWrapper.eq("t2.category3_id",category3Id);
       }
       switch (order){
           case 1:
               queryWrapper.orderByDesc("t1.sale_num");
               break;
           case 2:
               queryWrapper.orderByAsc("t1.sale_price");
               break;
           default:
               queryWrapper.orderByDesc("t1.sale_price");
       }
       List<ProductSku> productSkus = productSkuMapper.selectListByContion2(page,queryWrapper);
       PageInfo<ProductSku> pageInfo = new PageInfo<>();
       pageInfo.setPageNum(pageNum);
       pageInfo.setPageSize(pageSize);
       pageInfo.setTotal(page.getTotal());
       pageInfo.setList(productSkus);
       return pageInfo;
    }

    @Cacheable(value = "spzx:product",key = "'item:'+#skuId")
    @Override
    public ProductItemVO getItemBySkuId(Long skuId) {
        //1.
        ProductSku productSku = productSkuMapper.selectById(skuId);
        //2.
        Product product = baseMapper.selectById(productSku.getProductId());
        ProductVo2 productVo2 = new ProductVo2();
        BeanUtils.copyProperties(product,productVo2);
        List<ProductSku> productSkuList = productSkuMapper.selectList(Wrappers.lambdaQuery(ProductSku.class)
                .eq(ProductSku::getProductId,productSku.getProductId())
                .eq(ProductSku::getStatus,1));
        ProductVo2DTO productVo2DTO = productMapper.getByProductId(productSku.getProductId());
        productVo2DTO.setProductSkuList(productSkuList);
        BeanUtils.copyProperties(productVo2DTO,productVo2);
        //3.
        ProductDetails productDetails = productDetailsMapper.selectById(productSku.getProductId());
        //
        List<String> detailsImageUrlList = StringUtils.isEmpty(productDetails.getImageUrls())
                ?null:Arrays.asList(productDetails.getImageUrls().split(","));

        //4.
        List<String> sliderUrlList = StringUtils.isEmpty(product.getSliderUrls())
                ?null: Arrays.asList(product.getSliderUrls().split(","));
        //5.
        String specValue = product.getSpecValue();
        List<ProductSpecVO> specVOList = StringUtils.isEmpty(specValue)?
                null: JSON.parseArray(specValue,ProductSpecVO.class);

        //6.
        Map<String,Long> map = productSkuList.stream().collect(Collectors.toMap(ProductSku::getSkuSpec,ProductSku::getId));

        ProductItemVO productItemVO = new ProductItemVO();
        productItemVO.setProductSku(productSku);
        productItemVO.setProduct(productVo2);
        productItemVO.setDetailsImageUrlList(detailsImageUrlList);
        productItemVO.setSliderUrlList(sliderUrlList);
        productItemVO.setSpecValueList(specVOList);
        productItemVO.setSkuSpecValueMap(map);
        return productItemVO;
    }

    @Override
    public ProductSku getBySkuId(Long skuId) {
        return productSkuMapper.selectById(skuId);
    }

}
